//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: FormW2.cs
//
// Purpose: Installation & Configuration Wizard - screen 2 (Connection properties)
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace H2V2.ConfigUtility
{
	public class FormW2 : H2V2.ConfigUtility.FormWbase
	{
		private System.Windows.Forms.Label lbPwd;
		private System.Windows.Forms.Label lbUser;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox tbPwd;
		private System.Windows.Forms.TextBox tbUser;
		private System.Windows.Forms.TextBox tbDb;
		private System.Windows.Forms.TextBox tbServer;
		private System.Windows.Forms.CheckBox cbIntSec;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.ComponentModel.IContainer components = null;

		public FormW2()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lbPwd = new System.Windows.Forms.Label();
			this.lbUser = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.tbPwd = new System.Windows.Forms.TextBox();
			this.tbUser = new System.Windows.Forms.TextBox();
			this.tbDb = new System.Windows.Forms.TextBox();
			this.tbServer = new System.Windows.Forms.TextBox();
			this.cbIntSec = new System.Windows.Forms.CheckBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.textBox2);
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Name = "panel1";
			// 
			// panel2
			// 
			this.panel2.Name = "panel2";
			// 
			// btCancel
			// 
			this.btCancel.Name = "btCancel";
			this.btCancel.TabIndex = 6;
			// 
			// btNext
			// 
			this.btNext.Name = "btNext";
			this.btNext.TabIndex = 5;
			// 
			// lbPwd
			// 
			this.lbPwd.Enabled = false;
			this.lbPwd.Location = new System.Drawing.Point(136, 328);
			this.lbPwd.Name = "lbPwd";
			this.lbPwd.TabIndex = 38;
			this.lbPwd.Text = "Password:";
			this.lbPwd.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lbUser
			// 
			this.lbUser.Enabled = false;
			this.lbUser.Location = new System.Drawing.Point(136, 280);
			this.lbUser.Name = "lbUser";
			this.lbUser.TabIndex = 37;
			this.lbUser.Text = "User:";
			this.lbUser.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(136, 200);
			this.label2.Name = "label2";
			this.label2.TabIndex = 36;
			this.label2.Text = "Database:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(152, 160);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 23);
			this.label1.TabIndex = 35;
			this.label1.Text = "Server:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tbPwd
			// 
			this.tbPwd.Enabled = false;
			this.tbPwd.Location = new System.Drawing.Point(248, 328);
			this.tbPwd.MaxLength = 128;
			this.tbPwd.Name = "tbPwd";
			this.tbPwd.PasswordChar = '*';
			this.tbPwd.Size = new System.Drawing.Size(184, 20);
			this.tbPwd.TabIndex = 4;
			this.tbPwd.Text = "";
			// 
			// tbUser
			// 
			this.tbUser.Enabled = false;
			this.tbUser.Location = new System.Drawing.Point(248, 280);
			this.tbUser.MaxLength = 256;
			this.tbUser.Name = "tbUser";
			this.tbUser.Size = new System.Drawing.Size(184, 20);
			this.tbUser.TabIndex = 3;
			this.tbUser.Text = "";
			// 
			// tbDb
			// 
			this.tbDb.Location = new System.Drawing.Point(248, 200);
			this.tbDb.MaxLength = 256;
			this.tbDb.Name = "tbDb";
			this.tbDb.Size = new System.Drawing.Size(184, 20);
			this.tbDb.TabIndex = 1;
			this.tbDb.Text = "SQLH2Repository";
			// 
			// tbServer
			// 
			this.tbServer.Location = new System.Drawing.Point(248, 160);
			this.tbServer.MaxLength = 256;
			this.tbServer.Name = "tbServer";
			this.tbServer.Size = new System.Drawing.Size(184, 20);
			this.tbServer.TabIndex = 0;
			this.tbServer.Text = "(local)";
			// 
			// cbIntSec
			// 
			this.cbIntSec.Checked = true;
			this.cbIntSec.CheckState = System.Windows.Forms.CheckState.Checked;
			this.cbIntSec.Location = new System.Drawing.Point(248, 240);
			this.cbIntSec.Name = "cbIntSec";
			this.cbIntSec.Size = new System.Drawing.Size(136, 24);
			this.cbIntSec.TabIndex = 2;
			this.cbIntSec.Text = "Integrated Security";
			this.cbIntSec.CheckedChanged += new System.EventHandler(this.cbIntSec_CheckedChanged);
			// 
			// textBox1
			// 
			this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.textBox1.Location = new System.Drawing.Point(32, 16);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(496, 40);
			this.textBox1.TabIndex = 0;
			this.textBox1.TabStop = false;
			this.textBox1.Text = "Please choose a server to install SQLH2 Repository Database";
			// 
			// textBox2
			// 
			this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBox2.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.textBox2.Location = new System.Drawing.Point(40, 48);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(560, 64);
			this.textBox2.TabIndex = 1;
			this.textBox2.TabStop = false;
			this.textBox2.Text = "You can change default database name H2Repository to a name you prefer.\r\nIf datab" +
				"ase already exists it must not have any user objects (tables) in it.";
			// 
			// FormW2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(640, 478);
			this.Controls.Add(this.lbPwd);
			this.Controls.Add(this.lbUser);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.tbPwd);
			this.Controls.Add(this.tbUser);
			this.Controls.Add(this.tbDb);
			this.Controls.Add(this.tbServer);
			this.Controls.Add(this.cbIntSec);
			this.Name = "FormW2";
			this.Controls.SetChildIndex(this.panel2, 0);
			this.Controls.SetChildIndex(this.panel1, 0);
			this.Controls.SetChildIndex(this.cbIntSec, 0);
			this.Controls.SetChildIndex(this.tbServer, 0);
			this.Controls.SetChildIndex(this.tbDb, 0);
			this.Controls.SetChildIndex(this.tbUser, 0);
			this.Controls.SetChildIndex(this.tbPwd, 0);
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.label2, 0);
			this.Controls.SetChildIndex(this.lbUser, 0);
			this.Controls.SetChildIndex(this.lbPwd, 0);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void cbIntSec_CheckedChanged(object sender, System.EventArgs e)
		{
			lbUser.Enabled = !cbIntSec.Checked;
			tbUser.Enabled = !cbIntSec.Checked;
			lbPwd.Enabled = !cbIntSec.Checked;
			tbPwd.Enabled = !cbIntSec.Checked;
		}

		protected override void btNext_Click(object sender, System.EventArgs e)
		{
			bool ret;

			Cursor.Current = Cursors.WaitCursor;
			ret = MakeStep ();
			Cursor.Current = Cursors.Default;

			if (ret)
			{
				SetDefaultRep ();
				DialogResult = DialogResult.OK;
				Close ();
			}
		}

		bool MakeStep ()
		{
			bool ret = false;

			if (CheckConnectionArgs ()) 
			{
				if (InitInstaller ())
				{
					ret = true;
				}
			}

			return ret;
		}

		bool CheckConnectionArgs ()
		{
			string msg = "";
			bool ret = true;

			if (tbServer.Text == "")
			{
				msg = "You must specify Server name!";
				ret = false;
			}
			else if (tbDb.Text == "") 
			{
				msg = "You must specify Database name!";
				ret = false;
			}
			else if (!cbIntSec.Checked && tbUser.Text == "")
			{
				msg = "You must specify User name or use Integrated Security option!";
				ret = false;
			}
			
			if (!ret)
			{
				MessageBox.Show (msg, "Repository Installation", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return ret;
		}

		bool InitInstaller ()
		{
			bool ret = false;

			string scriptFile = ((MainForm.WizardMode) ? MainForm.BinPath : "")+"H2DBsetup.sql";

			try
			{
				if (cbIntSec.Checked)
				{
					MainForm.Dbi = new DbInstaller (tbServer.Text, tbDb.Text);
				}
				else
				{
					MainForm.Dbi = new DbInstaller (tbServer.Text, tbDb.Text, tbUser.Text, tbPwd.Text);
				}

				MainForm.Dbi.InitFile (scriptFile);
				ret = true;
			}
			catch (System.IO.FileNotFoundException fnf)
			{
				MessageBox.Show (fnf.Message, "Opening Script File", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (System.Data.SqlClient.SqlException sex)
			{
				MessageBox.Show (sex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			if (ret)
			{
				try
				{
					int r = MainForm.Dbi.CheckExistence ();

					if (r > 0) 
					{
						MessageBox.Show ("Database "+tbDb.Text+" already exists and has "+r+" user objects", "Checing Database existense", MessageBoxButtons.OK);
						ret = false;
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show (ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
					ret = false;
				}
			}

			if (!ret)
			{
				if (MainForm.Dbi != null) MainForm.Dbi.Close ();
			}

			return ret;
		}

		void SetDefaultRep ()
		{
			MainForm.stRep.Server = tbServer.Text;
			MainForm.stRep.Database = tbDb.Text;
			MainForm.stRep.bIntSec = cbIntSec.Checked;
			MainForm.stRep.User = tbUser.Text;
		}

	}
}

